package com.apestech.framework.xml.condition.node;

import com.apestech.formwork.condition.ConditionUtil;
import com.apestech.formwork.xml.XmlQuery;
import com.apestech.framework.json.JSON;
import com.apestech.framework.json.JSONObject;
import com.apestech.framework.xml.condition.modul.Condition;
import com.apestech.framework.xml.condition.modul.Control;
import com.apestech.framework.xml.condition.modul.Dimension;
import com.apestech.framework.xml.plugin.XmlPaste;
import com.apestech.framework.xml.pub.core.ElementUtil;
import com.apestech.framework.xml.pub.core.NodeJson;
import org.dom4j.Element;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hup
 * @create 2019-01-24-9:45
 */
public class NodeCondition extends ElementUtil {
    private Element node;
    private ArrayList<Dimension> dimensionList;
    private Control control;
    private Condition condition;
    private XmlQuery xmlQuery;

    public NodeCondition(Element node, XmlQuery xmlQuery){
        this.xmlQuery=xmlQuery;
        this.node=node;
        control=new Control();
        dimensionList=new ArrayList<Dimension>();
        condition=new Condition();
    }

    public void getData() throws Exception {
        condition.setCondition(getNodeAttr(node).getJSONObject("attrNode"), xmlQuery.getSession());
        List<?> list=node.content();
        int len=list.size();
        for(int i=0; i<len; i++){
            String scalss=list.get(i).getClass().getName();
            if(scalss.equals("org.dom4j.tree.DefaultElement")){
                getFieldList((Element) list.get(i));
            }
        }
        condition.setDimension(dimensionList);
        condition.setControl(control);
    }

    private void getFieldList(Element e) throws Exception {
        String theme=e.getName();
        if("control".equals(theme)){
            control.setControl(getNodeAttr(e).getJSONObject("attrNode"));
        }
        if("mapping".equals(theme)){
            condition.setMapping(getNodeAttr(e).getJSONObject("attrNode"));
        }
        if("tag".equals(theme)){
            condition.setTag(e.getTextTrim());
        }
        List<?> list=e.content();
        int len=list.size();
        for(int i=0; i<len; i++) {
            String scalss = list.get(i).getClass().getName();
            if (scalss.equals("org.dom4j.tree.DefaultElement")) {
                Element eml = (Element) list.get(i);
                JSONObject m=getNodeAttr(eml).getJSONObject("attrNode");
                if("dimensions".equals(theme)) {
                    String stop=m.getString("stop");
                    if(!"1".equals(stop)){
                        Dimension dimension=new Dimension(xmlQuery.getSession());
                        dimension.addField(m);
                        dimension.getFieldlist();
                        dimensionList.add(dimension);
                    }
                }
                else if("control".equals(theme)) control.addField(m);
                else if("master".equals(theme)) condition.setCuslist(m);
                else if("seqKey".equals(theme)){
                    condition.setSeq(getNodeAttr(eml).getJSONObject("attrNode"));
                }
                else if("form".equals(theme)){
                    NodeJson json=new NodeJson(eml);
                    json.expain();
                    JSONObject r=condition.getFormJSON();
                    if(r==null){
                        r=new JSONObject();
                    }
                    r.put(eml.getName().trim(), json.getResult());
                    condition.setFormJSON(r);
                }
            }
        }
    }

    public Condition getCondition() {
        return condition;
    }
}
